package com.bringway.webservice.http.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.webservice.http.service.MeetingBookingService;
import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月16日 下午1:47:19
* 类说明 会议预约
*/
@Controller
@RequestMapping("/hyyy")
public class MeetingBookingController extends JsonController {
	
	@Autowired
	private MeetingBookingService meetingBookingService;
	
	@RequestMapping("/queryCompanyList")
	public void queryCompanyList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		List<Map<String, Object>> list = meetingBookingService.queryCompanyList(params);
		serialize(request, response, list);
	}
	
	@RequestMapping("/queryMeetingBookingList")
	public void queryMeetingBookingList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		List<Map<String, Object>> list = meetingBookingService.queryMeetingBookingList(params);
		int count = meetingBookingService.getMeetingBookingCount(params);
		result.put("data", list);	
		result.put("total", count);
		serialize(request,response,result);
	}
	
	@RequestMapping("saveMeetingBookingList")
	public void saveMeetingBookingList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String companyId = RequestUtils.getStringParameter(request, "companyId");
		String companyName = RequestUtils.getStringParameter(request, "companyName");
		String companyJc = RequestUtils.getStringParameter(request, "companyJc");
		String bookingUserName = RequestUtils.getStringParameter(request, "bookingUserName");
		String bookingDate = RequestUtils.getStringParameter(request, "bookingDate");
		String bookingStartTime = RequestUtils.getStringParameter(request, "bookingStartTime");
		String bookingEndTime = RequestUtils.getStringParameter(request, "bookingEndTime");
		String tel = RequestUtils.getStringParameter(request, "tel");
		
//		String test,t2,t3;
//		test = new String(companyJc.getBytes(),"utf-8");
//		t2 = new String(companyJc.getBytes(),"gbk");
//		t3 = new String(companyJc.getBytes(),"gb2312");
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String msg = "";
		boolean isSuccess = false;
		boolean isValid = true;
		if(StringUtils.isEmpty(companyId)){
			isValid = false;
			msg = "1.公司名称不能为空!";
		}
		if(StringUtils.isEmpty(companyJc)){
			isValid = false;
			msg = "3.公司名称不能为空!";
		}
		if(StringUtils.isEmpty(bookingUserName)){
			isValid = false;
			msg = "预约人不能为空!";
		}
		if(StringUtils.isEmpty(bookingDate)){
			isValid = false;
			msg = "预约日期不能为空!";
		}
		if(StringUtils.isEmpty(bookingStartTime)){
			isValid = false;
			msg = "预约开始时间不能为空!";
		}
		if(StringUtils.isEmpty(bookingEndTime)){
			isValid = false;
			msg = "预约结束时间不能为空!";
		}
		if(StringUtils.isEmpty(tel)){
			isValid = false;
			msg = "联系方式不能为空!";
		}
		if(StringUtils.isNotEmpty(bookingStartTime) && StringUtils.isNotEmpty(bookingEndTime)){
			Date startTime = DateUtils.toDate(bookingStartTime, DateUtils.DEFAULT_TIME_FORMAT);
			Date endTime = DateUtils.toDate(bookingEndTime, DateUtils.DEFAULT_TIME_FORMAT);
			if(startTime.getTime() > endTime.getTime()){
				isValid = false;
				msg = "预约开始时间不能大于结束时间!";
			}
		}
		if(isValid){
			params.put("companyId", companyId);
			params.put("companyName", companyName);
			params.put("companyJc", companyJc);
			params.put("bookingUserName",bookingUserName);
			params.put("bookingDate", bookingDate);
			params.put("bookingStartTime", bookingStartTime);
			params.put("bookingEndTime", bookingEndTime);
			params.put("tel", tel);
			params.put("isValidSave", true);
			int count = meetingBookingService.getMeetingBookingCount(params);
			if(count > 0){
				isSuccess = false;
				msg = "对不起，该时间段已被预约！";
			}else{
				boolean isSave = meetingBookingService.saveMeetingBooking(params);
				if(isSave){
					isSuccess = true;
					msg = "预约成功!";
				}else{
					isSuccess = false;
					msg = "预约失败!";
				}
			}
		}
		result.put("success", isSuccess);
		result.put("msg", msg);
		serialize(request, response, result);
	}
	
}
